using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInventory : MonoBehaviour
{
    [SerializeField] private int _maxInventoryCount = 12;
    [SerializeField] private int _maxEquipWeaponCount = 4;
    [SerializeField] private List<ItemDataSO> _inventoryList = new();
    [SerializeField] private Dictionary<int, ItemDataSO> _equipWeaponDict = new();
    private Player_Stat _playerStat;

    private List<WeaponScrollDataSO> _allWeaponDataSOList = new();
    private List<WeaponScrollDataSO> _canSelectedWeapon = new();
    private List<WeaponScrollDataSO> _selectedWepaon = new();
    private void Awake()
    {
        _playerStat = GetComponent<Player_Stat>();
    }
    private void OnEnable()
    {
        Player.Instance.PlayerInput.Player.OpenInventory.performed += context => TriggerOpenInventoryUI();
        EventCenter.AddEventListener<ItemDataSO, int>(EventName.UI_EquipWeapon, HandleEquipWeapon);
        EventCenter.AddEventListener<ItemDataSO, int>(EventName.UI_CancelEquipment, HandleCancelEquipWeapon);
        EventCenter.AddEventListener<ItemDataSO>(EventName.UI_AddInventory, AddInventoryItem);
    }
    private void OnDisable()
    {
        Player.Instance.PlayerInput.Player.OpenInventory.performed -= context => TriggerOpenInventoryUI();
        EventCenter.RemoveEventListener<ItemDataSO, int>(EventName.UI_EquipWeapon, HandleEquipWeapon);
        EventCenter.RemoveEventListener<ItemDataSO, int>(EventName.UI_CancelEquipment, HandleCancelEquipWeapon);
        EventCenter.RemoveEventListener<ItemDataSO>(EventName.UI_AddInventory, AddInventoryItem);
    }
    private void TriggerOpenInventoryUI()
    {
        EventCenter.TriggerEvent(EventName.UI_OpenBackPackPanel, _inventoryList, _equipWeaponDict, _playerStat.GetPlayerStatGroup());
    }

    private void HandleEquipWeapon(ItemDataSO itemDataSO, int slotIndex)
    {
        RemoveInventoryItem(itemDataSO);
        AddEquipmentWeapon(itemDataSO, slotIndex);
        
        //更新挂载在玩家身上的武器
        EventCenter.TriggerEvent(EventName.UpdateHangedWepaon, _equipWeaponDict);

        TriggerOpenInventoryUI();
    }

    public void HandleCancelEquipWeapon(ItemDataSO itemDataSO, int slotIndex)
    {
        AddInventoryItem(itemDataSO);
        RemoveEquipmentWeapon(itemDataSO, slotIndex);

        //更新挂载在玩家身上的武器
        EventCenter.TriggerEvent(EventName.UpdateHangedWepaon, _equipWeaponDict);

        TriggerOpenInventoryUI();
    }
    public void AddInventoryItem(ItemDataSO itemDataSO)
    {
        if (CanAddInventoryItem() == false)
            return;

        _inventoryList.Add(itemDataSO);
    }
    private void RemoveInventoryItem(ItemDataSO itemDataSO)
    {
        _inventoryList.Remove(itemDataSO);
    }

    private void AddEquipmentWeapon(ItemDataSO itemDataSO, int slotIndex)
    {
        if (CanAddEquipmentItem() == false)
            return;

        _equipWeaponDict[slotIndex] = itemDataSO;
    }
    private void RemoveEquipmentWeapon(ItemDataSO itemDataSO, int slotIndex)
    {
        _equipWeaponDict.Remove(slotIndex);
    }

    private bool CanAddInventoryItem()
    {
        if (_inventoryList.Count >= _maxInventoryCount)
            return false;
        return true;
    }
    private bool CanAddEquipmentItem()
    {
        if (_equipWeaponDict.Count >= _maxEquipWeaponCount)
            return false;
        return true;
    }
    public List<WeaponScrollDataSO> GetAllWeaponDataSO()
    {
        _allWeaponDataSOList.Clear();
        foreach (var itemSO in _inventoryList)
        {
            if (itemSO is WeaponScrollDataSO weaponItemDataSO)
                _allWeaponDataSOList.Add(weaponItemDataSO);
        }
        foreach (var itemSO in _equipWeaponDict)
        {
            ItemDataSO itemDataSO = itemSO.Value;
            if (itemDataSO is WeaponScrollDataSO weaponItemDataSO)
                _allWeaponDataSOList.Add(weaponItemDataSO);
        }
        return _allWeaponDataSOList;
    }
    public List<WeaponScrollDataSO> GetCanSelectWeapon(WeaponScrollDataListSO weaponScrollDataListSO, int weaponSelectCount)
    {
        _canSelectedWeapon.Clear();
        _selectedWepaon.Clear();
        List<WeaponScrollDataSO> obtainedWeaponList = GetAllWeaponDataSO();

        WeaponScrollDataSO[] weapons = weaponScrollDataListSO.itemListData;

        for (int i = 0; i < weapons.Length; i++)
        {
            if (!obtainedWeaponList.Contains(weapons[i]))
                _canSelectedWeapon.Add(weapons[i]);
        }
        for (int i = 0; i < weaponSelectCount; i++)
        {
            int weaponCount = _canSelectedWeapon.Count;
            if (weaponCount == 0)
                break;

            int randomIndex = Random.Range(0, weaponCount);
            WeaponScrollDataSO weaponItemDataSO = _canSelectedWeapon[randomIndex];

            if (weaponItemDataSO != null)
            {
                _selectedWepaon.Add(weaponItemDataSO);
                _canSelectedWeapon.Remove(weaponItemDataSO);
            }
        }
        return _selectedWepaon;
    }
}
